#include "Graphics.hpp"

namespace zen {

	namespace video {

		std::map<GLenum, std::string> errorStringMap();


		glm::mat4 Graphics::modelView;
		glm::mat4 Graphics::projection;
		Material Graphics::material;
		int Graphics::activeTexture = 0;
		std::map<GLenum, std::string> Graphics::glErrorStrings = errorStringMap();


		std::map<GLenum, std::string> errorStringMap() {
			std::map<GLenum, std::string> map;
			map[GL_INVALID_ENUM] = "Invalid enumerator.";
			map[GL_INVALID_VALUE] = "Invalid value.";
			map[GL_INVALID_OPERATION] = "Invalid operation.";
			map[GL_INVALID_FRAMEBUFFER_OPERATION] = "Invalid framebuffer operation.";
			map[GL_STACK_OVERFLOW] = "Stack overflow.";
			map[GL_STACK_UNDERFLOW] = "Stack underflow.";
			map[GL_OUT_OF_MEMORY] = "Out of memory.";
			map[GL_TABLE_TOO_LARGE] = "Table too large.";
			return map;
		}

		std::string Graphics::getErrorString() {
			GLenum err = glGetError();
			if (err == GL_NO_ERROR) {
				return "Open GL errors: None.";
			} else {
				std::string res = "Open GL errors:\n";
				do {
					char hexbuf[9];
					_itoa_s(err, hexbuf, 9, 16);
					res += "\t[0x0" + std::string(hexbuf) + "] ";
					std::map<GLenum, std::string>::iterator iter = glErrorStrings.find(err);
					res += iter != glErrorStrings.end() ? iter->second : "Unknown error.";
					err = glGetError();
				} while (err != GL_NO_ERROR);
				return res;
			}
		}

	} // namespace video

} // namespace egl
